﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

using Ict.DataAccess;
using Ict.DataAccess.Linq;

namespace CSF.BusinessCore.BLL
{
    /// <summary>
    /// 业务逻辑层公用基类
    /// </summary>
    /// <typeparam name="TEntity">数据对象类类型</typeparam>
    public abstract class DataEntityLogic<TEntity>
    {
        /// <summary>
        /// 取某个数据对象在数据库中的所有数据
        /// </summary>
        /// <returns>数据对象列表</returns>
        public List<TEntity> GetAll()
        {
            var q = from p in DataContext.Select<TEntity>()
                    select p;
            return q.ToList();
        }

        /// <summary>
        /// 插入一条新的记录(全部字段更新)
        /// </summary>
        /// <param name="t">数据类实例</param>
        /// <returns>是否插入成功</returns>
        public bool Insert(TEntity t)
        {
            return Insert<TEntity>(t, null);
        }

        /// <summary>
        /// 插入一条新的记录(指定字段更新)
        /// </summary>
        /// <typeparam name="TElement">字段表类型</typeparam>
        /// <param name="t">数据类实例</param>
        /// <param name="selector">字段表</param>
        /// <returns>是否插入成功</returns>
        public bool Insert<TElement>(TEntity t, Expression<Func<TEntity, TElement>> selector)
        {
            using (Executor exec = DataContext.CreateExecutor())
            {
                return Insert(t, selector, exec);
            }
        }

        /// <summary>
        /// 使用指定执行器，插入一条新的记录(指定字段更新)
        /// </summary>
        /// <typeparam name="TElement">字段表类型</typeparam>
        /// <param name="t">数据类实例</param>
        /// <param name="selector">字段表</param>
        /// <param name="exec">执行器实例</param>
        /// <returns>是否插入成功</returns>
        public virtual bool Insert<TElement>(TEntity t, Expression<Func<TEntity, TElement>> selector, Executor exec)
        {
            var insertor = DataContext.Insert<TEntity>();
            ((ModifiableProvider)insertor.Provider).Executor = exec;

            bool bSave = insertor.Insert(t, selector);
            
            return bSave;
        }

        /// <summary>
        /// 更新符合条件的记录(全部字段更新)
        /// </summary>
        /// <param name="t">数据类实例</param>
        /// <param name="predicate">更新条件</param>
        /// <returns>更新记录数</returns>
        public int Update(TEntity t, Expression<Func<TEntity, bool>> predicate)
        {
            return Update<TEntity>(t, null, predicate);
        }

        /// <summary>
        /// 更新符合条件的记录(指定字段更新)
        /// </summary>
        /// <typeparam name="TElement">字段表类型</typeparam>
        /// <param name="t">数据类实例</param>
        /// <param name="selector">字段表</param>
        /// <param name="predicate">更新条件</param>
        /// <returns>更新记录数</returns>
        public int Update<TElement>(TEntity t,
            Expression<Func<TEntity, TElement>> selector,
            Expression<Func<TEntity, bool>> predicate)
        {
            using (Executor exec = DataContext.CreateExecutor())
            {
                return Update(t, selector, predicate, exec);
            }
        }

        /// <summary>
        /// 使用指定执行器，更新符合条件的记录(指定字段更新)
        /// </summary>
        /// <typeparam name="TElement">字段表类型</typeparam>
        /// <param name="t">数据类实例</param>
        /// <param name="selector">字段表</param>
        /// <param name="predicate">更新条件</param>
        /// <param name="exec">执行器实例</param>
        /// <returns>更新记录数</returns>
        public virtual int Update<TElement>(TEntity t,
            Expression<Func<TEntity, TElement>> selector,
            Expression<Func<TEntity, bool>> predicate,
            Executor exec)
        {
            var updator = DataContext.Update<TEntity>();
            ((ModifiableProvider)updator.Provider).Executor = exec;

            return updator.Update(t, selector, predicate);
        }

        /// <summary>
        /// 删除符合条件的记录
        /// </summary>
        /// <param name="predicate">删除条件</param>
        /// <returns>删除记录数</returns>
        public int Delete(Expression<Func<TEntity, bool>> predicate)
        {
            using (Executor exec = DataContext.CreateExecutor())
            {
                return Delete(predicate, exec);
            }
        }

        /// <summary>
        /// 使用指定执行器，删除符合条件的记录
        /// </summary>
        /// <param name="predicate">删除条件</param>
        /// <param name="exec">执行器实例</param>
        /// <returns>删除记录数</returns>
        public int Delete(Expression<Func<TEntity, bool>> predicate, Executor exec)
        {
            var deletor = DataContext.Delete<TEntity>();
            ((ModifiableProvider)deletor.Provider).Executor = exec;

            return deletor.Delete(predicate);
        }
    }
}
